﻿$(function () {
    ShowIn()
})
//显示采购信息
function ShowIn() {
    layui.use(function () {
        var table = layui.table;
        // 点击导出按钮
        document.getElementById('exportBtnD').addEventListener('click', function () {
            var checkStatus = table.checkStatus('PurchIntable');  // 获取当前表格的选中状态
            var selectedData = checkStatus.data;  // 获取选中的数据
            if (selectedData.length === 0) {
                alert('请先选择要导出的数据!');
                return;
            }
           
            var filteredData = selectedData.map(function (item) {
                // 创建一个新对象，只包含您想要的属性
                return {
                    '采购Id': item.purchaseID,
                    '采购编号': item.purchaseCode,
                    '入库编号': item.inventoryNo,
                    '原料编号': item.ingredientnumber,
                    '采购数量': item.purchaseNumber,
                    '采购日期': item.createdDate,
                    '状态': item.inventoryState,
                };
            });

            // 使用 XLSX 导出选中的数据
            var wb = XLSX.utils.book_new();
            var ws = XLSX.utils.json_to_sheet(filteredData); // 将选中的数据转换为工作表
            XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

            // 导出为 Excel 文件
            XLSX.writeFile(wb, '采购列表.xlsx');
        });
       

        var PurchCode = $("#PurchCode1").val();
        var CreatedDate = $("#CreateTime").val();
        var InventoryState = $("#InventoryState1").val();

        // 先进行表格的初始渲染
        table.render({
            elem: '#PurchIntable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'purchaseCode', title: '采购编号' },
                { field: 'inventoryNo', title: '入库编号' },

                { field: 'ingredientnumber', title: '原料编号' },
                { field: 'purchaseNumber', title: '采购数量' },
                {
                    field: 'createdDate', title: '采购日期',
                    templet: function (d) {
                        // 假设createdDate字段是一个日期字符串，格式为'YYYY-MM-DD HH:MM:SS'
                        // 截取前10位以获取日期部分.substring(0, 10)
                        var dateStr = d.createdDate;
                        // 返回包含日期的HTML字符串（这里可以简单地返回日期字符串，不需要额外的HTML标签）
                        return dateStr;
                    }
                },
                { field: 'inventoryState', title: '状态' },

                {
                    field: '', title: '操作', templet: function (d) {
                        if (d.inventoryState === '待入库') {
                            return '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="showData1(' + d.purchaseID + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                                '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.purchaseID + ')"><i class="layui-icon layui-icon-edit"></i>入库</a> ' +
                                '<a href="javascript:void(0);" class="layui-btn layui-btn-danger layui-btn-xs disabled" onclick="return false;"><i class="layui-icon layui-icon-delete"></i>删除（禁用）</a>';
                        } 
                        else {
                            return '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs" onclick="showData2(' + d.purchaseID + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                                '<a href="javascript:void(0);" class="layui-btn layui-btn-edit layui-btn-xs disabled" onclick="return false;"><i class="layui-icon layui-icon-edit"></i>入库（禁用）</a> ' +
                                '<a href="javascript:void(0);" class="layui-btn layui-btn-danger layui-btn-xs" onclick="DelData(' + d.purchaseID + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                        }
                    }

                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });

        // 创建表格实例
        $.ajax({
            url: "../Purch/PInfoList",
            type: "get",
            data: {
                PageIndex: 1,
                PageSize: 100,
                token: "",
                PurchaseCode: PurchCode,
                CreatedDate: CreatedDate,
                InventoryState: InventoryState
            },

            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.list);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('PurchIntable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.list
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });

    });

}
// 单删数据的函数
window.DelData = function DelData(purchaseID) {
    layer.confirm('确定要删除该条数据吗？', function () {
        $.ajax({
            type: "post",
            url: "../Purch/PurchDelete",  // 替换为实际的后台删除数据接口地址
            data: { PurchaseID: purchaseID, token: localStorage.getItem("token") },
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                if (res.code == 1) {
                    layer.alert("删除成功");
                    ShowIn();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });

    });
};
//入库
window.editData = function editData(purchaseID) {
    localStorage['purchaseID1'] = purchaseID;

    //localStorage.setItem("purchaseID", purchaseID)
    //console.log(localStorage.getItem("purchaseID"))
    location.href = '../Warehouse/InventoryAdd';

}
//查看待入库详情跳转
window.showData1 = function showData1(purchaseID) {

    localStorage['purchaseID'] = purchaseID;

    location.href = '/Purch/RuPurchs';

}
//查看已入库跳转
window.showData2 = function showData2(purchaseID) {

    localStorage['purchaseID'] = purchaseID;

    location.href = '/Purch/KuYiPurch';

}